package com.situ.mvc.controller;

import java.nio.file.attribute.UserPrincipalLookupService;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.situ.mvc.common.ServerResponse;
import com.situ.mvc.entity.User;
import com.situ.mvc.service.IUserService;
import com.situ.mvc.service.impl.UserServiceImpl;

import javafx.stage.PopupBuilder;
import sun.print.resources.serviceui;

@Controller
@RequestMapping(value="/user")
public class UserController {
	private IUserService userService = new UserServiceImpl(); 

	/*@InitBinder
	public void initBinder(WebDataBinder binder) {
		// 把日期的字符串类型转换成Date
		SimpleDateFormat simpleDateFormat = 
				new SimpleDateFormat("yyyy-MM-dd");
		simpleDateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, 
				new CustomDateEditor(simpleDateFormat, true));
	}*/
	
	@RequestMapping(value="/getUserLoginPage")
	public String getUserLoginPage() {
		return "user_login";
	}
	
	@RequestMapping(value="/login")
	@ResponseBody
	public ServerResponse login(String name, String password) {
		ServerResponse response = userService.login(name, password);
		return response;
	}
	
	@RequestMapping(value="/getUserListPage")
	public String getUserListPage() {
		return "user_list";
	}
	
	@RequestMapping(value="/pageList")
	@ResponseBody
	public ServerResponse pageList(Integer page, Integer limit) {
		ServerResponse response = userService.pageList(page, limit);
		return response;
	}
	
	@RequestMapping(value="/deleteById")
	@ResponseBody
	public ServerResponse deleteById(Integer id) {
		ServerResponse response = userService.deleteById(id);
		return response;
	}
	
	@RequestMapping(value="/getUserAddPage")
	public String getUserAddPage() {
		return "user_add";
	}
	
	@RequestMapping(value="/add")
	@ResponseBody
	public ServerResponse add(User user) {
		ServerResponse response = userService.add(user);
		return response;
	}
	
	@RequestMapping(value="/getUserUpdatePage")
	public String getUserUpdatePage(Integer id, Model model) {
		User user = userService.findById(id);
		model.addAttribute("user", user);
		return "user_update";
	}
	
	@RequestMapping(value="/update")
	@ResponseBody
	public ServerResponse update(User user) {
		ServerResponse response = userService.update(user);
		return response;
	}
	
	@RequestMapping(value="/deleteAll")
	@ResponseBody
	public ServerResponse deleteAll(String ids) {
		ServerResponse response = userService.deleteAll(ids);
		return response;
	}
}
